package cn.tedu.straw.portal.security;

import cn.tedu.straw.portal.service.IUserService;
import cn.tedu.straw.portal.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserDetailsServiceImpl  implements UserDetailsService {

    @Autowired
    IUserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名得到登录的详情
        //假设容许登录的用户名是admin,密码的原文是123456
//        if(username==null||!"admin".equals(username)){
//            return null;
//        }
//        UserDetails userDetails= User.builder()
//                .username("admin")
//                .password("{bcrypt}$2a$10$q6sTLG13Ufje/FZDdSzniuYK/eqptQOSgSCn7/BJoLeQjPry4WSpG")
//                .accountLocked(false)
//                .disabled(false)
//                .authorities("查看用户","创建用户","修改用户")
//                .build();
        if(username==null){
            return null;
        }
        UserLoginVO userLoginVO=userService.getLoginUserInfo(username);
        if (userLoginVO==null){
            return null;
        }
//        String[] authorities=new String[userLoginVO.getPermissions().size()];
//        for (int i=0;i<authorities.length;i++){
//            authorities[i]=userLoginVO.getPermissions().get(i).getAuthority();
//        }
        List<SimpleGrantedAuthority> authorities=new ArrayList<>();
        for(int i=0;i<userLoginVO.getPermissions().size();i++){
            String authority=userLoginVO.getPermissions().get(i).getAuthority();
            SimpleGrantedAuthority  simpleGrantedAuthority=new SimpleGrantedAuthority(authority);
            authorities.add(simpleGrantedAuthority);
        }
        LoginUserInfo loginUserInfo=new LoginUserInfo(
                userLoginVO.getPhone(),
                userLoginVO.getPassword(),
                userLoginVO.getIsEnabled()==1,
                true,
                true,
                userLoginVO.getIsLocked()==0,
                authorities
        );
        loginUserInfo.setId(userLoginVO.getId());
        loginUserInfo.setNickname(userLoginVO.getNickname());
        loginUserInfo.setPhone(userLoginVO.getPhone());
        loginUserInfo.setAccountType(userLoginVO.getAccountType());
        return loginUserInfo;
    }
}
